var mapInitialized = false;

function show(nr) {
    var i = 1;
    document.getElementById("TabContent" + nr).style.display = "block";
    document.getElementById("navi" + nr).className = "selectedNaviBox";
    
    x = document.getElementById('2');
    /*
    if (nr == 2) {
        initLoader();
    }
    */
    for (i = 1;i < 4;i++) {
        if (i != nr) {
            document.getElementById("TabContent" + i).style.display = "none";
            document.getElementById("navi" + i).className = "naviBox";
        }
    }
}

function clickOnSearchButton(e) {
    if (window.event) {
        e = window.event;
    }
    if (e.keyCode == 13) {
        document.getElementById('searchLocationButton').click();
    }
}

function switchToKey(e) {
    if (window.event) {
        e = window.event;
    }
    if (e.keyCode == 40) {
        document.getElementById('exampleId').focus();
    }
}

var t;
var liIndex =  - 1;
var oldIndex =  - 1;

function resetIndex() {
    liIndex =  - 1;
}

function keyEvent(e) {
    if (window.event) {
        e = window.event;
    }

    var b = (window.event) ? window.event.keyCode : e.keyCode;
    var rt = 13;
    var au = 38;
    var ad = 40;
    var pu = 33;
    var pd = 34;
    var ul = document.getElementById("as_ul");

    switch (b) {
        case au:
            if (!ul) {
                return false;
            }

            if (liIndex >  - 1)
                ul.childNodes[liIndex].className = "";

            liIndex = liIndex - 1 <=  - 1 ? ul.childNodes.length - 1 : liIndex - 1;
            ul.childNodes[liIndex].className = "as_highlight";
            var c1 = ul.childNodes[liIndex];
            c1.onclick();
            break;
        case ad:
            if (!ul) {
                return false;
            }

            if (liIndex >  - 1)
                ul.childNodes[liIndex].className = "";

            liIndex = liIndex + 1 >= ul.childNodes.length ? 0 : liIndex + 1;
            ul.childNodes[liIndex].className = "as_highlight";
            var c2 = ul.childNodes[liIndex];
            c2.onclick();
            break;
        case rt:
            hideLocationSearchResults();
            break;
        default :
            searchLocations();
    }
}

function showLocationSearchResults() {
    var srh = window.document.getElementById('searchResultsHtml');
    var sra = window.document.getElementById('searchResultsArea');
    sra.innerHTML = srh.value;
    sra.style = "width: 400px; display: block;"
}

function hideLocationSearchResults() {
    var sra = window.document.getElementById('searchResultsArea');
    sra.innerHTML = "";
    sra.style = "width: 0px; display: none;"
}

var xhr;

function initXHR() {
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
}

function searchLocations() {
    resetIndex();
    window.clearTimeout(t);
    t = window.setTimeout("searchForLocations();", 500);
}

function searchForLocations() {
    var query = document.getElementById('locSearchInput').value;
    var sll = map.getCenter();

    var url = "/locationSearch?q=" + encodeURIComponent(query) + "&sll=" + encodeURIComponent(sll);
    sendRequest(url, processAjaxCall);
}

function sendRequest(url, handler) {
    initXHR();
    xhr.onreadystatechange = handler;
    xhr.open("GET", url, true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    xhr.send(null);
}

function processAjaxCall() {
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
            var sra = window.document.getElementById('searchResultsArea');
            sra.innerHTML = xhr.responseText;
            sra.style = "width: 400px; display: block;"
        }
    }
}

function changeLength() {
    var i = 150 - document.getElementById('MFMessageBox').value.length;
    document.getElementById('MFMessageLength').innerHTML = i;    
}

function MFEvent_hideAllDateSelections() {
    var month = document.getElementById('MFEventMonth');
    var day = document.getElementById('MFEventDay');
    var at = document.getElementById('MFEventAt');
    var hour = document.getElementById('MFEventHour');
    var min = document.getElementById('MFEventMinute');    
    var colon = document.getElementById('MFEventColon');
    
    month.style.visibility = "hidden";
    month.style.display = "none";

    day.style.visibility = "hidden";
    day.style.display = "none";
        
    at.style.visibility = "hidden";        
    at.style.display = "none";
    
    hour.style.visibility = "hidden";        
    hour.style.display = "none";
        
    min.style.visibility = "hidden";
    min.style.display = "none";
        
    colon.style.visibility = "hidden";
    colon.style.display = "none";
}

function MFEvent_showHideDateDetails() {
    var i = document.getElementById('MFDateSelect').value;
    var month = document.getElementById('MFEventMonth');
    var day = document.getElementById('MFEventDay');
    var at = document.getElementById('MFEventAt');
    var hour = document.getElementById('MFEventHour');
    var min = document.getElementById('MFEventMinute');
    var colon = document.getElementById('MFEventColon');        
    
    MFEvent_hideAllDateSelections();
    if (i < 6 && i != 2) {
        hour.style.visibility = "visible";
        hour.style.display = "block";                
        min.style.visibility = "visible";
        min.style.display = "block";         
        at.style.visibility = "visible";
        at.style.display = "block";         
        colon.style.visibility = "visible";
        colon.style.display = "block";                
    }    
    else
    if (i == 6) {        
        month.style.visibility = "visible";
        month.style.display = "block";        
        day.style.visibility = "visible";
        day.style.display = "block";        
        at.style.visibility = "visible";
        at.style.display = "block";         
        hour.style.visibility = "visible";
        hour.style.display = "block";       
        colon.style.visibility = "visible";        
        colon.style.display = "block";
        min.style.visibility = "visible";
        min.style.display = "block";                
    }    
}